################################################################################
# Project:  Lib KML
# Purpose:  CMake build scripts
# Author:   Dmitry Baryshnikov, dmitry.baryshnikov@nexgis.com
################################################################################
# Copyright (C) 2018, NextGIS <info@nextgis.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
################################################################################

set(LIBKML_DATA_DIR  ${CMAKE_SOURCE_DIR}/testdata CACHE "Directory containing test data" PATH)
add_definitions("-DDATADIR=\"${LIBKML_DATA_DIR}\"")

include_directories(
    ${PROJECT_SOURCE_DIR}/src
    ${PROJECT_SOURCE_DIR}/src/kml/base/contrib
    ${CMAKE_CURRENT_SOURCE_DIR}
)

include_directories(${Boost_INCLUDE_DIRS})

set(WITH_GTest ON CACHE BOOL "Google test on")
set(WITH_GTest_EXTERNAL ON CACHE BOOL "Google test external on")
find_anyproject(GTest REQUIRED)

FILE(GLOB extra_dom_src  ${PROJECT_SOURCE_DIR}/src/kml/dom/*.cc)
FILE(GLOB extra_engine_src  ${PROJECT_SOURCE_DIR}/src/kml/engine/*.cc)

set(extra_src ${PROJECT_SOURCE_DIR}/src/kml/base/math_util.cc
              ${PROJECT_SOURCE_DIR}/src/kml/base/attributes.cc
              ${PROJECT_SOURCE_DIR}/src/kml/base/string_util.cc
              ${PROJECT_SOURCE_DIR}/src/kml/base/expat_handler_ns.cc
              ${PROJECT_SOURCE_DIR}/src/kml/base/expat_parser.cc
              ${PROJECT_SOURCE_DIR}/src/kml/base/file_posix.cc
              ${PROJECT_SOURCE_DIR}/src/kml/base/file.cc
              ${PROJECT_SOURCE_DIR}/src/kml/base/file_win32.cc
              ${PROJECT_SOURCE_DIR}/src/kml/base/date_time.cc
              ${PROJECT_SOURCE_DIR}/src/kml/base/csv_splitter.cc
        ${PROJECT_SOURCE_DIR}/src/kml/base/referent.cc
        ${PROJECT_SOURCE_DIR}/src/kml/base/time_util.cc
        ${PROJECT_SOURCE_DIR}/src/kml/base/version.cc
        ${PROJECT_SOURCE_DIR}/src/kml/base/uri_parser.cc
        ${PROJECT_SOURCE_DIR}/src/kml/base/xml_namespaces.cc
        ${PROJECT_SOURCE_DIR}/src/kml/base/zip_file.cc
        ${PROJECT_SOURCE_DIR}/src/kml/base/contrib/strptime.c
        ${PROJECT_SOURCE_DIR}/src/kml/base/contrib/minizip/ioapi.c
        ${PROJECT_SOURCE_DIR}/src/kml/base/contrib/minizip/iomem_simple.c
        ${PROJECT_SOURCE_DIR}/src/kml/base/contrib/minizip/unzip.c
        ${PROJECT_SOURCE_DIR}/src/kml/base/contrib/minizip/zip.c
        ${PROJECT_SOURCE_DIR}/src/kml/convenience/feature_list.cc
        ${PROJECT_SOURCE_DIR}/src/kml/convenience/convenience.cc
#        ${PROJECT_SOURCE_DIR}/src/kml/dom/kml_factory.cc
        ${extra_dom_src}
        ${extra_engine_src}
        )

function(build_test)
  cmake_parse_arguments(TEST  "" "GROUP;NAME" "DEPENDS" ${ARGN} )
  add_executable(${TEST_GROUP}_${TEST_NAME}_test ${TEST_NAME}_test.cc ${extra_src})
  add_dependencies(${TEST_GROUP}_${TEST_NAME}_test ${TEST_DEPENDS})
  set(TEST_LINKS)
  foreach(TEST_D ${TEST_DEPENDS})
    get_target_property(LINK_PROP ${TEST_D} LINK_INTERFACE_LIBRARIES)
    if(LINK_PROP)
      list(APPEND TEST_LINKS ${LINK_PROP})
    endif()
  endforeach()
#  target_link_libraries(${TEST_GROUP}_${TEST_NAME}_test ${TEST_LINKS} ${TEST_DEPENDS} ${GTEST_LIBRARY} ${EXPAT_LIBRARY}
#          ${Boost_LIBRARIES})
  target_link_extlibraries(${TEST_GROUP}_${TEST_NAME}_test)
  set_target_properties(${TEST_GROUP}_${TEST_NAME}_test PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
  add_test(${TEST_GROUP}_${TEST_NAME} ${CMAKE_BINARY_DIR}/bin/${TEST_GROUP}_${TEST_NAME}_test)



  if(WIN32)
      set(IMPORTED_WIN_DLLS ${CMAKE_BINARY_DIR}/third-party/install/bin/libgtest.dll ${CMAKE_BINARY_DIR}/third-party/install/bin/libgtest_main.dll)
      foreach(IMPORTED_WIN_DLL ${IMPORTED_WIN_DLLS})
          add_custom_command(TARGET ${TEST_GROUP}_${TEST_NAME}_test
                              POST_BUILD
                              COMMAND ${CMAKE_COMMAND} -E copy_if_different ${IMPORTED_WIN_DLL} ${CMAKE_BINARY_DIR}/bin
                              COMMAND ${CMAKE_COMMAND} -E copy_if_different ${IMPORTED_WIN_DLL} ${CMAKE_BINARY_DIR}/bin/Release    # For Windows
                              COMMAND ${CMAKE_COMMAND} -E copy_if_different ${IMPORTED_WIN_DLL} ${CMAKE_BINARY_DIR}/bin/Debug      # For Windows
                              COMMENT "Copy ${IMPORTED_WIN_DLL} to test bin directory"
                          )
      endforeach()
  endif()
endfunction(build_test)

add_subdirectory(kml)
